package com.mryao.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeoutException;

public class EmitLogDirect {
    private static final String[] SEVERITES = {"info","warning","error"};
    public static void main (String[] argv) throws IOException, TimeoutException {
        //获取工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置ip
        factory.setHost("192.168.231.132");
        factory.setUsername("admin");
        factory.setPassword("admin");
        //打开连接
        Connection connection = factory.newConnection();
        //打开通道
        Channel channel = connection.createChannel();
        //声明一个扇形的exchange
        channel.exchangeDeclare("dir_logs_queue", "direct");
        for(int i=0; i<10; i++) {
            String severity = getSeverity();
            String message = severity + new Date().toString();
            //把消息发给exchange
            channel.basicPublish("dir_logs_queue", severity, null, message.getBytes("UTF-8"));
            System.out.println("[x] Sent " + message);
        }
        //释放资源
        channel.close();
        connection.close();
    }

    private static String getSeverity(){
        Random random = new Random();
        int i = random.nextInt(3);
        return SEVERITES[i];
    }
}
